﻿Imports QLPhuTungOto.Data
Imports QLPhuTungOto.Bus
Public Class ucQLNhapHang

    Dim PT_BUS As New PhuTungBUS
    Dim dt As New DataTable
    Dim pt As New PhuTung
    Dim PN_BUS As New PhieuNhapBUS

    Dim TrangHienTai As Integer = 1
    Dim TongSoTrang As Integer = 1
    Dim SoDongTrenTrang As Integer = 0

    Private Sub ucQLNhapHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            UcPhanTrang1.txtSoDongCuaTrang.Text = HamHoTro.DocThongSo("config.config", "PhanTrangNhapHang", "200")
            SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)
            TinhSoTrang()
            TaoCauTrucBang()
            LoadDSPhuTung()
        Catch ex As Exception

        End Try

    End Sub

    Private Sub LoadDSPhuTung()
        SoDongTrenTrang = Integer.Parse(Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text))
        gcDSPhuTung.DataSource = PT_BUS.LayBang((TrangHienTai - 1) * SoDongTrenTrang, SoDongTrenTrang)
        UcPhanTrang1.lblTrangHienTai.Text = TrangHienTai.ToString + "/" + TongSoTrang.ToString
    End Sub

    Private Sub lblThemPhuTungMoi_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblThemPhuTungMoi.LinkClicked
        Dim frm As New frmThemPT
        frm.ShowDialog()
        LoadDSPhuTung()
    End Sub
    
    Private Sub gvDSPhuTung_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSPhuTung.FocusedRowChanged

        Try
            Dim selectedIndex As Integer = gvDSPhuTung.FocusedRowHandle
            txtGiaNhap.Text = gvDSPhuTung.GetRowCellValue(selectedIndex, "GiaNhap")
            txtGiaBan.Text = gvDSPhuTung.GetRowCellValue(selectedIndex, "GiaBan")
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub btnChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChon.Click
        Try
            Dim err As String = ""
            err = KiemTraNhap()

            If err = "" Then
                ChonPhuTung()
                TinhTong()
            Else
                MessageBox.Show(err, "Chú ý")
            End If

        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub TaoCauTrucBang()
        dt.Columns.Add("Stt")
        dt.Columns.Add("MaPhuTung")
        dt.Columns.Add("TenPhuTung")
        dt.Columns.Add("GiaNhap")
        dt.Columns.Add("GiaBan")
        dt.Columns.Add("SoLuongNhap")
        dt.Columns.Add("GiaTri")
    End Sub

    Private Sub ChonPhuTung()
        pt = PT_BUS.LayTheoMa(gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "MaPhuTung"))
        Dim dr As DataRow = dt.NewRow
        dr("Stt") = pt.Stt
        dr("MaPhuTung") = pt.MaPhuTung
        dr("TenPhuTung") = pt.TenPhuTung
        dr("GiaNhap") = txtGiaNhap.Text 'pt.GiaNhap
        dr("GiaBan") = txtGiaBan.Text 'pt.GiaBan
        dr("SoLuongNhap") = nudSoLuong.Value
        dr("GiaTri") = Double.Parse(txtGiaNhap.Text) * nudSoLuong.Value

        For i = 0 To dt.Rows.Count - 1

            If dt.Rows(i)("MaPhuTung") = pt.MaPhuTung Then
                dt.Rows(i)("SoLuongNhap") = Integer.Parse(dt.Rows(i)("SoLuongNhap")) + nudSoLuong.Value
                dt.Rows(i)("GiaTri") = Double.Parse(dt.Rows(i)("SoLuongNhap")) * Double.Parse(txtGiaNhap.Text)
                dt.Rows(i)("GiaNhap") = txtGiaNhap.Text
                dt.Rows(i)("GiaBan") = txtGiaBan.Text
                gcDSNhap.DataSource = dt
                Exit Sub
            End If

        Next

        dt.Rows.Add(dr)
        gcDSNhap.DataSource = dt
    End Sub

    Private Sub gvDSNhap_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSNhap.FocusedRowChanged

    End Sub

    Private Sub btnBoChon_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBoChon.Click
        Try
            BoChonPhuTung()
            TinhTong()
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub BoChonPhuTung()

        For Each r As DataRow In dt.Rows

            If r("MaPhuTung").ToString = gvDSNhap.GetRowCellValue(gvDSNhap.FocusedRowHandle, "MaPhuTung").ToString Then
                dt.Rows.Remove(r)
                Exit For
            End If

        Next

        gcDSNhap.DataSource = dt
    End Sub

    Private Sub btnHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuyBo.Click
        Try
            dt.Clear()
            gcDSNhap.DataSource = dt

            lblThanhTien.Text = "0"
        Catch ex As Exception

        End Try
        
    End Sub

    Private Function KiemTraNhap() As String
        Dim err As String = ""

        If txtGiaBan.Text.Trim.Length = 0 Then
            err += "Bạn chưa nhập ""Giá bán""" + ControlChars.NewLine
        End If
        If txtGiaNhap.Text.Trim.Length = 0 Then
            err += "Bạn chưa nhập ""Giá nhập""" + ControlChars.NewLine
        End If

        Return err
    End Function

    Private Sub btnNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhap.Click
        Try
            If gvDSNhap.RowCount > 0 Then
                Nhap()
                MessageBox.Show("Đã tạo phiếu nhập.", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
                dt.Clear()
                gcDSNhap.DataSource = dt

                lblThanhTien.Text = "0"
                LoadDSPhuTung()
            End If
        Catch ex As Exception
            MessageBox.Show("Không thể tạo phiếu nhập", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub Nhap()
        Dim lst_ds_ChiTietPhieuNhap As New List(Of ChiTietPhieuNhap)
        For i = 0 To dt.Rows.Count - 1
            Dim ctpn As New ChiTietPhieuNhap With {.MaPhuTung = dt.Rows(i)("MaPhuTung").ToString(),
                                                  .SoLuong = Integer.Parse(dt.Rows(i)("SoLuongNhap")),
                                                  .Gia = Double.Parse(dt.Rows(i)("GiaNhap")),
                                                  .ThanhTien = Double.Parse(dt.Rows(i)("GiaTri"))}
            lst_ds_ChiTietPhieuNhap.Add(ctpn)
        Next

        PN_BUS.ThemPhieuNhap(lst_ds_ChiTietPhieuNhap, txtGhiChuPhieuNhap.Text)
        ThemSoLuongPhuTung()
        CapNhatGiaPhuTung()
    End Sub

    Private Sub ThemSoLuongPhuTung()

        For i = 0 To dt.Rows.Count - 1
            PT_BUS.ThemSoLuongPhuTung(dt.Rows(i)("MaPhuTung"), Integer.Parse(dt.Rows(i)("SoLuongNhap")))
        Next

    End Sub

    Private Sub CapNhatGiaPhuTung()

        For i = 0 To dt.Rows.Count - 1
            Dim ma As String = dt.Rows(i)("MaPhuTung").ToString
            Dim gianhap As Double = Double.Parse(dt.Rows(i)("GiaNhap"))
            Dim giaban As Double = Double.Parse(dt.Rows(i)("GiaBan"))

            PT_BUS.CapNhatGiaPhuTung(ma, gianhap, giaban)
        Next

    End Sub

    Private Sub TinhTong()

        Dim tong As Double = 0
        For i = 0 To dt.Rows.Count - 1
            tong += Double.Parse(dt.Rows(i)("GiaTri".ToString()))
        Next
        lblThanhTien.Text = tong.ToString()
    End Sub

    Private Sub txtGiaNhap_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGiaNhap.KeyPress

        If Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
            
        Else
            e.Handled = True
        End If

    End Sub

    Private Sub txtGiaBan_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGiaBan.KeyPress
        If Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
        Else

            e.Handled = True
        End If
    End Sub

    Private Sub CapNhatStatus()
        ToolStripStatusLabel1.Text = "Tổng số lượng: " + PT_BUS.DemSoLuongPhuTung().ToString
    End Sub

    Private Sub gvDSPhuTung_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvDSPhuTung.DataSourceChanged
        CapNhatStatus()
    End Sub

    Private Sub UcPhanTrang1_SuaSoDongTrenTrang() Handles UcPhanTrang1.SuaSoDongTrenTrang
        Try
            SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)

            If SoDongTrenTrang > 0 Then
                TinhSoTrang()
                TrangHienTai = 1
                LoadDSPhuTung()
                HamHoTro.GhiThongSo("config.config", "PhanTrangNhapHang", UcPhanTrang1.txtSoDongCuaTrang.Text.Trim)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangCuoi_Click() Handles UcPhanTrang1.TrangCuoi_Click
        Try
            TrangHienTai = TongSoTrang
            LoadDSPhuTung()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangDau_Click() Handles UcPhanTrang1.TrangDau_Click
        Try
            TrangHienTai = 1
            LoadDSPhuTung()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangSau_Click() Handles UcPhanTrang1.TrangSau_Click
        Try
            If TrangHienTai < TongSoTrang Then
                TrangHienTai += 1
                LoadDSPhuTung()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangTruoc_Click() Handles UcPhanTrang1.TrangTruoc_Click
        Try
            If TrangHienTai > 1 Then
                TrangHienTai -= 1
                LoadDSPhuTung()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function TinhSoTrang() As Integer
        Dim st As Integer = 0
        Try

            Dim soluongpt As Integer = PT_BUS.DemSoLuongPhuTung
            st = (soluongpt \ SoDongTrenTrang)

            If soluongpt Mod SoDongTrenTrang <> 0 Then
                st += 1
            End If

            TongSoTrang = st

        Catch ex As Exception

        End Try
        Return st
    End Function

    Private Sub LabelControl2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles LabelControl2.Paint
        Try

            If TrangThai.ThayDoiPhuTung_NhapHang Then
                TinhSoTrang()
                LoadDSPhuTung()
                TrangThai.ThayDoiPhuTung_NhapHang = False
            End If

        Catch ex As Exception

        End Try
    End Sub
End Class
